package com.xframe.xdal.core.manage;

import com.xframe.xdal.core.XDataSource;
import com.xframe.xdal.core.model.mapping.*;

import java.util.List;
import java.util.Map;

/**
 * 功能描述：db管理基类
 * 创建人：xufeng
 * 创建时间：2022/11/4 18:36
 *
 * @version 1.0
 **/
public abstract class BaseDbManage {
    /**
     * 数据源配置
     */
    protected XDataSource dataSource;

    public BaseDbManage(XDataSource dataSource) {
        this.dataSource = dataSource;
    }


    // List<DBTableInfo> dbTableInfoList = dataSource.executeQuery(getTableSql(), paramList, DBTableInfo.class);

    /**
     * 获取所有表信息
     *
     * @return
     */
    public abstract List<DBTableInfo> getTableInfoList();

    /**
     * 获取表列信息
     *
     * @return
     */
    public abstract List<DBColumnInfo> getDbColumnInfoList(String tableName);


    public Map<String, DBTableInfo> getTableInfoMap() {
        return this.getTableInfoMap(TableKeyMode.KEY);
    }

    /**
     * 获取所有表信息
     *
     * @return
     */
    public abstract Map<String, DBTableInfo> getTableInfoMap(TableKeyMode tableKeyMode);

    /**
     * 根据指定的表名获取表信息
     *
     * @param tableName        表名
     * @param isIncludeColumns 是否包含列信息
     * @return
     */
    public abstract DBTableInfo getTableInfo(String tableName, boolean isIncludeColumns);

    /**
     * 生成表结构
     *
     * @param tableInfo  ITableInfo 对象
     * @param isRecreate 是否重新生成表结构
     * @return
     */
    public abstract boolean createTable(ITableInfo tableInfo, boolean isRecreate) throws Exception;

    public abstract boolean deleteTable(ITableInfo tableInfo) throws Exception;



    /**
     * 生成历史表结构
     *
     * @param tableInfo ITableInfo 需要生成历史表的对象
     * @return
     */
    public abstract boolean createHisTable(ITableInfo tableInfo) throws Exception;

    public enum TableKeyMode {
        KEY,    // MAYSQL 使用  tableID
        TABLENAME   // 表名称
    }

    public static BaseDbManage create(XDataSource dataSource) {
        switch (dataSource.getDataSourceTypeEnums()) {
            case MySql:
                return new MysqlDBManage(dataSource);
            case Oracle:
                return new OracleDBManage(dataSource);
            default:
                return null;
        }
    }

    /**
     * 封装历史表信息
     *
     * @param sourceTableInfo    ITableInfo 需要生成历史表的对象
     * @param hisTableName 历史表表名
     * @return
     */
    public ITableInfo packHisTableInfo(ITableInfo sourceTableInfo, String hisTableName) {

        String tablePrefix = "his_";
        String columnPrefix = "his_";

        TableInfo tableInfo = new TableInfo(hisTableName,sourceTableInfo.getCode(), tablePrefix + sourceTableInfo.getAsName(),
                tablePrefix + sourceTableInfo.getLabel(),sourceTableInfo.getIndexName(),sourceTableInfo.getDataSourceDefCode());

        ITableInfo hisTableInfo = tableInfo;

        //设置历史表原始主键信息
        IColumnMapping pkFirst = sourceTableInfo.findPkFirst();
        String pkName = pkFirst.getName();
        String hisPkName = columnPrefix + pkName;

        //封装原始主键信息
        List<IColumnMapping> iColumnMappingList = sourceTableInfo.getColumnMappingList();
        IColumnMapping iColumnMapping = ColumnMapping.valueOf(hisPkName, "", false, pkFirst.getDataType(), hisPkName, pkFirst.getLength());
        //((ColumnMapping) iColumnMapping).setTableInfo(hisTableInfo);
        iColumnMappingList.add(0, iColumnMapping);

        hisTableInfo.setColumnMappingList(iColumnMappingList);

        return hisTableInfo;
    }
}
